##################################################################
################################################################
### REPLICATION FILE: STATE POLICY AND NATIONAL REPRESENTATION ### 
###############################################################
##################################################################

### DIRECTORY, PACKAGES, ETC ### 


rm(list=ls())

library(plm)
library(stargazer)
library(AER)
library(gsynth)
library(estimatr)
library(panelView)
library(stargazer)
library(tidyverse)
library(rio)
library(reshape2)
library(lmtest)
library(multiwayvcov)
library(ivpack)
library(reshape2)
library(gridExtra)
library(ggrepel)



#setwd
setwd("C:/Users/samtr/Dropbox/Dissertation/Marijuana/writing_congress_paper/lsq_submission/dataverse")

###########################
#### IMPORT AND CLEAN DATA #####
##########################

#import marijuana opinion and ideology measures (STATE-YEAR)
opinions <- import("opinions.xlsx")

#import marijuana revenue in 2018 (STATE IN 2018)
revenue_by_state_2018 <- import("revenue_by_state_2018.xlsx")

#marijuana policy data (STATE-YEAR)
policy.dat <- import("mj_policy.dta")

#vote shares
pres_votes <- import("pres_votes.xlsx")

#covariates 
covs <- left_join(pres_votes, subset(opinions, year>=2000), by = c("abb"="abb")) %>%
  group_by(abb) %>%
  dplyr::summarize(pres_dem_2016 = mean(pres_dem_2016),
            pres_dem_2012 = mean(pres_dem_2012),
            pres_dem_2008 = mean(pres_dem_2008),
            social_lib2000s = mean(mass_liberalism_soc[year < 2010 & year >= 2000]),
            econ_lib2000s = mean(mass_liberalism_econ[year < 2010 & year >= 2000]),
            opinion2000s = mean(opinion[year < 2010 & year >= 2000]),
            opinion2010 = mean(opinion[year==2010]),
            pop = mean(pop)
  )

#regions
covs$region = ifelse(covs$abb %in% c("CT","ME","MA","NH","RI","VT","NJ","NY","PA"), "northeast",
                     ifelse(covs$abb %in% c("IN","IL","MI","OH","WI","IA","KS","MN","MO","NE","ND","SD"), "midwest",
                            ifelse(covs$abb %in% c("DE","DC","FL","GA","MD","NC","SC","VA","WV","AL","KY","MS","TN","AR","LA","OK","TX"), "south",
                                   ifelse(covs$abb %in% c("AZ","CO","ID","NM","MT","UT","NV","WY","AK","CA","HI","OR","WA"), "west", NA))))

#merging covariates with policy data
policy.dat <- left_join(policy.dat, covs, by=c("abb"="abb"))
policy.dat <- left_join(policy.dat, opinions, by = c("abb", "year"))


#import sponsorship data from 116th Congress 
dat116 <- import("mj_116th_data.dta")

#remove POTUS
dat116 <- subset(dat116, congress=="116th" & chamber != "President")

#join with other data sources 
dat116 <- left_join(dat116, subset(policy.dat, year==2020), by=c("abb"="abb")) #policy status at end of 116th Congress. 
dat116 <- left_join(dat116, revenue_by_state_2018, by=c("abb"="abb"))

#mj revenue per capita
dat116$rev_per_cap <- dat116$tot_revenue_2018/dat116$pop

#generate bill sponsorship score 
dat116$tot_mj = (dat116$hr1595_sponsor + dat116$hr3884_sponsor + dat116$hr420_sponsor + dat116$hr1120_sponsor + dat116$hr1118_sponsor + dat116$hr2093_sponsor + dat116$hr2012_sponsor +
                   dat116$hr1456_sponsor + dat116$hr1151_sponsor + dat116$hr712_sponsor +
                   dat116$hr493_sponsor + dat116$hr601_sponsor + dat116$hr127_sponsor + dat116$hr1455_sponsor)/14



#########################
#### CONSTRUCTING FIGURE 1 ##############
###########################

policy.dat.2020 <- subset(policy.dat, year==2020)

#rec market
rec <- lm(rec_law ~ referenda, data = policy.dat.2020)
rec_est <- coeftest(rec)[2,1]
rec_se <- coeftest(rec)[2,2]

#medical market
med <- lm(medical_law ~ referenda, data = policy.dat.2020)
med_est <- coeftest(med)[2,1]
med_se <- coeftest(med)[2,2]

#scale items to 0-1
policy.dat.2020$opinion2000s <- with(policy.dat.2020, 
                                               (opinion2000s - min(opinion2000s))/(max(opinion2000s - min(opinion2000s))))
policy.dat.2020$social_lib2000s_scaled <- with(policy.dat.2020, 
                                               (social_lib2000s - min(social_lib2000s))/(max(social_lib2000s - min(social_lib2000s))))
policy.dat.2020$econ_lib2000s_scaled <- with(policy.dat.2020, 
                                               (econ_lib2000s - min(econ_lib2000s))/(max(econ_lib2000s - min(econ_lib2000s))))
dat116$nominate_dim1_scaled <- with(dat116, 
                                               (nominate_dim1 - min(nominate_dim1,na.rm=T))/(max(nominate_dim1,na.rm=T) - min(nominate_dim1,na.rm=T)))
dat116$nominate_dim2_scaled <- with(dat116, 
                                               (nominate_dim2 - min(nominate_dim2,na.rm=T))/(max(nominate_dim2,na.rm=T) - min(nominate_dim2,na.rm=T)))

#mj attitudes in 2000s
attitudes <-  lm(opinion2000s ~ referenda, data = policy.dat.2020)
attitudes_est <- coeftest(attitudes)[2,1]
attitudes_se <- coeftest(attitudes)[2,2]

#social liberalism
social_lib <- lm(social_lib2000s_scaled ~ referenda, data = policy.dat.2020) 
social_lib_est <- coeftest(social_lib)[2,1]
social_lib_se <- coeftest(social_lib)[2,2]

#econ liberalism
econ_lib <- lm(econ_lib2000s_scaled ~ referenda, data = policy.dat.2020) 
econ_lib_est <- coeftest(econ_lib)[2,1]
econ_lib_se <- coeftest(econ_lib)[2,2]

#election 2016 
pres16 <- lm(pres_dem_2016 ~ referenda, data = policy.dat.2020)
pres16_est <- coeftest(pres16)[2,1]
pres16_se <- coeftest(pres16)[2,2]

#DWnominate
dim1 <- lm_robust(nominate_dim1_scaled ~ referenda, clusters = abb, data = dat116)
dim1_est <- coeftest(dim1)[2,1]
dim1_se <- coeftest(dim1)[2,2]

dim2 <- lm_robust(nominate_dim2_scaled ~ referenda, clusters = abb, data = dat116)
dim2_est <- coeftest(dim2)[2,1]
dim2_se <- coeftest(dim2)[2,2]

#plotting Fig1
num = 7
est = c(attitudes_est, social_lib_est, pres16_est, dim1_est, dim2_est, rec_est, med_est)
se = c(attitudes_se, social_lib_se, pres16_se, dim1_se, dim2_se, rec_se, med_se)
lower.conf = est - 1.96*se
upper.conf = est + 1.96*se
x = c(1:num)
plotdata = data.frame(est, se,lower.conf, upper.conf,x)

fig1 = ggplot(plotdata, aes(x = x, y = est))+
  geom_point(size=3)+
  geom_linerange(aes(ymin=est-1.96*se, ymax=est+1.96*se), linetype = 1, size = 1)+
  #geom_linerange(aes(ymin=est-1.645*se, ymax=est+1.645*se), linetype = 1, size = 1.5)+
  coord_flip()+
  geom_hline(yintercept = 0, linetype = "dashed")+
  theme_bw()+
  theme(text = element_text(size=16))+
  labs(x = "", y = "Bivariate association")+
  scale_y_continuous(breaks=c(-.25, 0,.25, .5))+
  scale_x_continuous(breaks = 1:num, labels = c("Marijuana legalization \n favorability 2000-2010", "Social liberalism 2000-2010 \n (mass public)","2016 presidential vote \n (Democratic)","DW-NOMINATE \n dimension 1 \n 116th Congress","DW-NOMINATE \n dimension 2 \n 116th Congress",
                                                "Adult-Use \n marijuana (2020)", "Medical \n marijuana (2020)"))+
  theme(legend.position = "none")
fig1

#tidy
rm(attitudes, dim1, dim2, econ_lib, med, plotdata, pres12, pres16, rec, iv.confirm, pop)


################################
#### FIRST STAGE RESULTS (TABLE A1) ######
################################

stage1 <- lm(status ~ referenda, data = dat116)
stage1covs <- lm(status ~ referenda + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, data = dat116)

#stargazer for table 
names = c("Ballot initiative", "Democratic PID", "DW-NOMINATE \n dimension 1", "DW-NOMINATE \n dimension2",
          "2016 Democratic \n presidential vote share", "Social liberalism 2000s")

#generate table A1
stargazer(stage1, stage1covs, 
          no.space = T, title = "",
          digits = 2,
          covariate.labels=names,
          dep.var.caption  = "Recreational Sales",
          dep.var.labels.include = FALSE,
          #keep.stat="n",
          omit = c("time","Constant")
)

#################################
#### BILL SPONSORSHIP IV RESULTS ################
################################

##### DESCRIPTIVE ANALYSIS (FIG2 AND FIGA2) #####

#collapse sponsorship by bill and party
plotdata <- dat116 %>%
  group_by(party, status) %>%
  dplyr::summarize(hr1595_sponsor = mean(hr1595_sponsor),
            hr2093_sponsor = mean(hr2093_sponsor),
            hr3884_sponsor = mean(hr3884_sponsor))

#reshape
plotdata <- melt(plotdata, id.vars=c("party","status"), variable.name = "bills", 
                 value.name = "sponsor")
#clean
plotdata <- subset(plotdata, party != "")
plotdata$bills2 <- c(rep("SAFE", 6), rep("STATES",6), rep("MORE", 6))

fig2 <- ggplot(data = plotdata, aes(x = status))+
  geom_col(aes(y=sponsor, fill=party), position = "dodge") + 
  facet_wrap(~bills2)+
  labs(x = "", y = "Proportion sponsoring")+
  scale_x_continuous(breaks = 0:2, labels = c("No Legal \n Marijuana","Medical","Adult \n Use"))+
  scale_fill_manual(values=c("blue", "red"))+
  theme_bw()+
  theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1))+
  theme(text = element_text(size=16))+
  theme(legend.position = "none")
fig2


#distribution of bill sponsorship score
jitter <- position_jitter(width = 0.1, height = .05)
dat116$pid2 <- ifelse(dat116$party=="D", "Democrats", "Republicans")
figA2 <- ggplot(data = dat116, aes(y = tot_mj, x = status)) +
  geom_point(position=jitter, shape = 1, col = "darkgrey")+
  facet_wrap(~pid2)+
  labs(x = "", y = "NCIA Sponsorship Score")+
  theme_bw()+
  scale_x_continuous(breaks = 0:2, labels = c("No Legal \n Marijuana","Medical","Adult-Use"))+
  theme(text = element_text(size=16))+
  theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1))+
  theme(legend.position = "none")
figA2

##### IV ANALYSIS (FIG3) ##### 

#sponsorship score
tot_mj <- ivreg(tot_mj ~ status | referenda , data = dat116)
summary(tot_mj, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(tot_mj, dat116$abb)
tot_mj.est = cluster.robust.se(tot_mj, dat116$abb)[2,1]
tot_mj.se = cluster.robust.se(tot_mj, dat116$abb)[2,2]

#safe banking act
safe <- ivreg(hr1595_sponsor ~ status | referenda , data = dat116)
summary(safe, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(safe, dat116$abb)
safe.est = cluster.robust.se(safe, dat116$abb)[2,1]
safe.se = cluster.robust.se(safe, dat116$abb)[2,2]

#states act
states <- ivreg(hr2093_sponsor ~ status | referenda , data = dat116)
summary(states, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(states, dat116$abb)
states.est = cluster.robust.se(states, dat116$abb)[2,1]
states.se = cluster.robust.se(states, dat116$abb)[2,2]

#more act
more <- ivreg(hr3884_sponsor ~ status | referenda , data = dat116)
summary(more, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(more, dat116$abb)
more.est = cluster.robust.se(more, dat116$abb)[2,1]
more.se = cluster.robust.se(more, dat116$abb)[2,2]

### with covariates ###
dat116.est <- dat116 %>%
  select(tot_mj, hr1595_sponsor, hr2093_sponsor, hr3884_sponsor, 
         status, dem, referenda, nominate_dim1, nominate_dim2, pres_dem_2016, social_lib2000s, abb)

#sponsorship score
tot_mj2 <- ivreg(tot_mj ~ status + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s | 
                   referenda + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, data = na.omit(dat116.est))
summary(tot_mj2, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(tot_mj2, na.omit(dat116.est)$abb)
tot_mj2.est = cluster.robust.se(tot_mj2, na.omit(dat116.est)$abb)[2,1]
tot_mj2.se = cluster.robust.se(tot_mj2, na.omit(dat116.est)$abb)[2,2]

#safe banking act
safe2 <- ivreg(hr1595_sponsor ~ status + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s | 
                 referenda + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, data = na.omit(dat116.est))
summary(safe2, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(safe2, na.omit(dat116.est)$abb)
safe2.est = cluster.robust.se(safe2, na.omit(dat116.est)$abb)[2,1]
safe2.se = cluster.robust.se(safe2, na.omit(dat116.est)$abb)[2,2]


#states act
states2 <- ivreg(hr2093_sponsor ~ status + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s | 
                   referenda + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, data = na.omit(dat116.est))
summary(states2, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(states2, na.omit(dat116.est)$abb)
states2.est = cluster.robust.se(states2, na.omit(dat116.est)$abb)[2,1]
states2.se = cluster.robust.se(states2, na.omit(dat116.est)$abb)[2,2]

#more act
more2 <- ivreg(hr3884_sponsor ~ status + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s | 
                 referenda + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, data = na.omit(dat116.est))
summary(more2, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(more2, na.omit(dat116.est)$abb)
more2.est = cluster.robust.se(more2, na.omit(dat116.est)$abb)[2,1]
more2.se = cluster.robust.se(more2, na.omit(dat116.est)$abb)[2,2]


#preparing to plot
est = c(safe.est, safe2.est, states.est, states2.est, more.est, more2.est, tot_mj.est, tot_mj2.est)
se = c(safe.se, safe2.se, states.se, states2.se, more.se, more2.se, tot_mj.se, tot_mj2.se)
lower.conf = est - 1.96*se
upper.conf = est + 1.96*se

type = c(rep("SAFE Act", 2), rep("STATES Act", 2), rep("MORE Act",2), rep("Composite",2))
covs = rep(c("N","Y"), 4)
x <- c(.95, 1.05, 1.95, 2.05, 2.95, 3.05, 3.95, 4.05)
order = (c(rep(1,2), rep(2,2),rep(3,2),rep(4,2)))
plotdata = data.frame(est, se, lower.conf, upper.conf, covs, type,order)
plotdata = plotdata[order(-order),]
plotdata = data.frame(plotdata, x)
plotdata$covariates <- ifelse(plotdata$covs=="N", "No", "Yes")

#plotting
fig3 = ggplot(plotdata, aes(x = x, y = est))+
  geom_point(size=3, aes(shape = covariates, color = covariates))+
  geom_linerange(aes(ymin=lower.conf, ymax=upper.conf, color=covariates), linetype = 1, size = 1)+
  coord_flip()+
  geom_hline(yintercept = 0, linetype = "dashed")+
  theme_bw()+
  labs(x = "", y = "")+
  scale_y_continuous(breaks=c(0,.25,.5,.75,1))+
  scale_x_continuous(breaks = 1:4, labels = c("Sponsorship \n score", "MORE", "STATES", "SAFE"))+
  theme(text = element_text(size=16))+
  scale_color_manual(values=c('grey','black'))+
  theme(legend.position = "right")
fig3


##### HETEROGENEOUS EFFECTS BY CHAMBER (FIGA8) #####

#restrict to senate
dat116.est.nohouse <- dat116 %>%
  filter(chamber=="Senate") %>%
  select(tot_mj, hr1595_sponsor, hr2093_sponsor, hr3884_sponsor, 
         status, dem, referenda, nominate_dim1, nominate_dim2, pres_dem_2016, social_lib2000s, abb)

#bill sponsorship score
tot_mj <- ivreg(tot_mj ~ status | referenda , data = na.omit(dat116.est.nohouse))
summary(tot_mj, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(tot_mj, dat116.est.nohouse$abb)
tot_mj.est = cluster.robust.se(tot_mj, dat116.est.nohouse$abb)[2,1]
tot_mj.se = cluster.robust.se(tot_mj, dat116.est.nohouse$abb)[2,2]

#safe banking act
safe <- ivreg(hr1595_sponsor ~ status | referenda , data = na.omit(dat116.est.nohouse))
summary(safe, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(safe, dat116.est.nohouse$abb)
safe.est = cluster.robust.se(safe, dat116.est.nohouse$abb)[2,1]
safe.se = cluster.robust.se(safe, dat116.est.nohouse$abb)[2,2]

#states act
states <- ivreg(hr2093_sponsor ~ status | referenda , data = na.omit(dat116.est.nohouse))
summary(states, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(states, dat116.est.nohouse$abb)
states.est = cluster.robust.se(states, dat116.est.nohouse$abb)[2,1]
states.se = cluster.robust.se(states, dat116.est.nohouse$abb)[2,2]

#more act
more <- ivreg(hr3884_sponsor ~ status | referenda , data = na.omit(dat116.est.nohouse))
summary(more, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(more, dat116.est.nohouse$abb)
more.est = cluster.robust.se(more, dat116.est.nohouse$abb)[2,1]
more.se = cluster.robust.se(more, dat116.est.nohouse$abb)[2,2]


### with covariates ###

#bill sponsorship score
tot_mj2 <- ivreg(tot_mj ~ status + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s | 
                   referenda + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, data = na.omit(dat116.est.nohouse))
summary(tot_mj2, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(tot_mj2, na.omit(dat116.est.nohouse)$abb)
tot_mj2.est = cluster.robust.se(tot_mj2, na.omit(dat116.est.nohouse)$abb)[2,1]
tot_mj2.se = cluster.robust.se(tot_mj2, na.omit(dat116.est.nohouse)$abb)[2,2]

#safe banking act
safe2 <- ivreg(hr1595_sponsor ~ status + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s | 
                 referenda + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, data = na.omit(dat116.est.nohouse))
summary(safe2, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(safe2, na.omit(dat116.est.nohouse)$abb)
safe2.est = cluster.robust.se(safe2, na.omit(dat116.est.nohouse)$abb)[2,1]
safe2.se = cluster.robust.se(safe2, na.omit(dat116.est.nohouse)$abb)[2,2]

#states act
states2 <- ivreg(hr2093_sponsor ~ status + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s | 
                   referenda + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, data = na.omit(dat116.est.nohouse))
summary(states2, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(states2, na.omit(dat116.est.nohouse)$abb)
states2.est = cluster.robust.se(states2, na.omit(dat116.est.nohouse)$abb)[2,1]
states2.se = cluster.robust.se(states2, na.omit(dat116.est.nohouse)$abb)[2,2]

#more act
more2 <- ivreg(hr3884_sponsor ~ status + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s | 
                 referenda + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, data = na.omit(dat116.est.nohouse))
summary(more2, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(more2, na.omit(dat116.est.nohouse)$abb)
more2.est = cluster.robust.se(more2, na.omit(dat116.est.nohouse)$abb)[2,1]
more2.se = cluster.robust.se(more2, na.omit(dat116.est.nohouse)$abb)[2,2]


est = c(safe.est, safe2.est, states.est, states2.est, more.est, more2.est, tot_mj.est, tot_mj2.est)
se = c(safe.se, safe2.se, states.se, states2.se, more.se, more2.se, tot_mj.se, tot_mj2.se)
lower.conf = est - 1.96*se
upper.conf = est + 1.96*se

type = c(rep("SAFE Act", 2), rep("STATES Act", 2), rep("MORE Act",2), rep("Composite",2))
covs = rep(c("N","Y"), 4)
x = c(.9,1.1,1.9,2.1,2.9,3.1,3.9,4.1)
x <- c(.95, 1.05, 1.95, 2.05, 2.95, 3.05, 3.95, 4.05)
order = (c(rep(1,2), rep(2,2),rep(3,2),rep(4,2)))
plotdata = data.frame(est, lower.conf, upper.conf, covs, type,order)
plotdata = plotdata[order(-order),]
plotdata.senate = data.frame(plotdata, x)

#restrict to house
dat116.est.nosenate <- dat116 %>%
  filter(chamber=="House") %>%
  select(tot_mj, hr1595_sponsor, hr2093_sponsor, hr3884_sponsor, 
         status, dem, referenda, nominate_dim1, nominate_dim2, pres_dem_2016, social_lib2000s, abb)

#bill sponsorship score
tot_mj <- ivreg(tot_mj ~ status | referenda , data = na.omit(dat116.est.nosenate))
summary(tot_mj, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(tot_mj, dat116.est.nosenate$abb)
tot_mj.est = cluster.robust.se(tot_mj, dat116.est.nosenate$abb)[2,1]
tot_mj.se = cluster.robust.se(tot_mj, dat116.est.nosenate$abb)[2,2]

#safe banking act
safe <- ivreg(hr1595_sponsor ~ status | referenda , data = na.omit(dat116.est.nosenate))
summary(safe, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(safe, dat116.est.nosenate$abb)
safe.est = cluster.robust.se(safe, dat116.est.nosenate$abb)[2,1]
safe.se = cluster.robust.se(safe, dat116.est.nosenate$abb)[2,2]

#states act
states <- ivreg(hr2093_sponsor ~ status | referenda , data = na.omit(dat116.est.nosenate))
summary(states, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(states, dat116.est.nosenate$abb)
states.est = cluster.robust.se(states, dat116.est.nosenate$abb)[2,1]
states.se = cluster.robust.se(states, dat116.est.nosenate$abb)[2,2]

#more act
more <- ivreg(hr3884_sponsor ~ status | referenda , data = na.omit(dat116.est.nosenate))
summary(more, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(more, dat116.est.nosenate$abb)
more.est = cluster.robust.se(more, dat116.est.nosenate$abb)[2,1]
more.se = cluster.robust.se(more, dat116.est.nosenate$abb)[2,2]


### with covariates ###

#bill sponsorship score
tot_mj2 <- ivreg(tot_mj ~ status + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s | 
                   referenda + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, data = na.omit(dat116.est.nosenate))
summary(tot_mj2, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(tot_mj2, na.omit(dat116.est.nosenate)$abb)
tot_mj2.est = cluster.robust.se(tot_mj2, na.omit(dat116.est.nosenate)$abb)[2,1]
tot_mj2.se = cluster.robust.se(tot_mj2, na.omit(dat116.est.nosenate)$abb)[2,2]

#safe banking act
safe2 <- ivreg(hr1595_sponsor ~ status + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s | 
                 referenda + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, data = na.omit(dat116.est.nosenate))
summary(safe2, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(safe2, na.omit(dat116.est.nosenate)$abb)
safe2.est = cluster.robust.se(safe2, na.omit(dat116.est.nosenate)$abb)[2,1]
safe2.se = cluster.robust.se(safe2, na.omit(dat116.est.nosenate)$abb)[2,2]

#states act
states2 <- ivreg(hr2093_sponsor ~ status + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s | 
                   referenda + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, data = na.omit(dat116.est.nosenate))
summary(states2, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(states2, na.omit(dat116.est.nosenate)$abb)
states2.est = cluster.robust.se(states2, na.omit(dat116.est.nosenate)$abb)[2,1]
states2.se = cluster.robust.se(states2, na.omit(dat116.est.nosenate)$abb)[2,2]

#more act
more2 <- ivreg(hr3884_sponsor ~ status + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s | 
                 referenda + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, data = na.omit(dat116.est.nosenate))
summary(more2, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(more2, na.omit(dat116.est.nosenate)$abb)
more2.est = cluster.robust.se(more2, na.omit(dat116.est.nosenate)$abb)[2,1]
more2.se = cluster.robust.se(more2, na.omit(dat116.est.nosenate)$abb)[2,2]


#preparing to plot 
est = c(safe.est, safe2.est, states.est, states2.est, more.est, more2.est, tot_mj.est, tot_mj2.est)
se = c(safe.se, safe2.se, states.se, states2.se, more.se, more2.se, tot_mj.se, tot_mj2.se)
lower.conf = est - 1.96*se
upper.conf = est + 1.96*se

type = c(rep("SAFE Act", 2), rep("STATES Act", 2), rep("MORE Act",2), rep("Composite",2))
covs = rep(c("N","Y"), 4)
x = c(.9,1.1,1.9,2.1,2.9,3.1,3.9,4.1)
x <- c(.95, 1.05, 1.95, 2.05, 2.95, 3.05, 3.95, 4.05)
order = (c(rep(1,2), rep(2,2),rep(3,2),rep(4,2)))
plotdata = data.frame(est, lower.conf, upper.conf, covs, type,order)
plotdata = plotdata[order(-order),]
plotdata.house = data.frame(plotdata, x)
plotdata.house$chamber <- "House"
plotdata.senate$chamber <- "Senate"
plotdata <- rbind(plotdata.house, plotdata.senate)
plotdata$covariates <- ifelse(plotdata$covs=="N", "No", "Yes")

#plotting
figA8 <- ggplot(plotdata, aes(x = x, y = est))+
  geom_point(size=3, aes(shape = covariates, color = covariates))+
  geom_linerange(aes(ymin=lower.conf, ymax=upper.conf, color=covariates), linetype = 1, size = 1)+
  facet_wrap(~chamber)+
  coord_flip()+
  geom_hline(yintercept = 0, linetype = "dashed")+
  theme_bw()+
  labs(x = "", y = "")+
  scale_y_continuous(breaks=c(0,.25,.5,.75,1))+
  scale_x_continuous(breaks = 1:4, labels = c("Sponsorship \n score", "MORE", "STATES", "SAFE"))+
  theme(text = element_text(size=16))+
  scale_color_manual(values=c('grey','black'))+
  theme(legend.position = "right")
figA8



##### EXCLUDING THE WEST (FIGA3) #####

#bill sponsorship score
tot_mj <- ivreg(tot_mj ~ status | referenda , data = subset(dat116, region!="west"))
summary(tot_mj, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(tot_mj, subset(dat116, region!="west")$abb)
tot_mj.est = cluster.robust.se(tot_mj, subset(dat116, region!="west")$abb)[2,1]
tot_mj.se = cluster.robust.se(tot_mj, subset(dat116, region!="west")$abb)[2,2]

#safe banking act
safe <- ivreg(hr1595_sponsor ~ status | referenda , data = subset(dat116, region!="west"))
summary(safe, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(safe, subset(dat116, region!="west")$abb)
safe.est = cluster.robust.se(safe, subset(dat116, region!="west")$abb)[2,1]
safe.se = cluster.robust.se(safe, subset(dat116, region!="west")$abb)[2,2]

#states act
states <- ivreg(hr2093_sponsor ~ status | referenda , data = subset(dat116, region!="west"))
summary(states, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(states, subset(dat116, region!="west")$abb)
states.est = cluster.robust.se(states, subset(dat116, region!="west")$abb)[2,1]
states.se = cluster.robust.se(states, subset(dat116, region!="west")$abb)[2,2]

#more act
more <- ivreg(hr3884_sponsor ~ status | referenda , data = subset(dat116, region!="west"))
summary(more, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(more, subset(dat116, region!="west")$abb)
more.est = cluster.robust.se(more, subset(dat116, region!="west")$abb)[2,1]
more.se = cluster.robust.se(more, subset(dat116, region!="west")$abb)[2,2]


### with covariates ###
dat116.est.nowest <- dat116 %>%
  filter(region!="west") %>%
  select(tot_mj, hr1595_sponsor, hr2093_sponsor, hr3884_sponsor, 
         status, dem, referenda, nominate_dim1, nominate_dim2, pres_dem_2016, social_lib2000s, abb)


#bill sponsorship score
tot_mj2 <- ivreg(tot_mj ~ status + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s | 
                   referenda + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, data = na.omit(dat116.est.nowest))
summary(tot_mj2, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(tot_mj2, na.omit(dat116.est.nowest)$abb)
tot_mj2.est = cluster.robust.se(tot_mj2, na.omit(dat116.est.nowest)$abb)[2,1]
tot_mj2.se = cluster.robust.se(tot_mj2, na.omit(dat116.est.nowest)$abb)[2,2]

#safe banking act
safe2 <- ivreg(hr1595_sponsor ~ status + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s | 
                 referenda + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, data = na.omit(dat116.est.nowest))
summary(safe2, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(safe2, na.omit(dat116.est.nowest)$abb)
safe2.est = cluster.robust.se(safe2, na.omit(dat116.est.nowest)$abb)[2,1]
safe2.se = cluster.robust.se(safe2, na.omit(dat116.est.nowest)$abb)[2,2]

#states act
states2 <- ivreg(hr2093_sponsor ~ status + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s | 
                   referenda + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, data = na.omit(dat116.est.nowest))
summary(states2, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(states2, na.omit(dat116.est.nowest)$abb)
states2.est = cluster.robust.se(states2, na.omit(dat116.est.nowest)$abb)[2,1]
states2.se = cluster.robust.se(states2, na.omit(dat116.est.nowest)$abb)[2,2]

#more act
more2 <- ivreg(hr3884_sponsor ~ status + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s | 
                 referenda + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, data = na.omit(dat116.est.nowest))
summary(more2, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(more2, na.omit(dat116.est.nowest)$abb)
more2.est = cluster.robust.se(more2, na.omit(dat116.est.nowest)$abb)[2,1]
more2.se = cluster.robust.se(more2, na.omit(dat116.est.nowest)$abb)[2,2]


#preparing to plot 
est = c(safe.est, safe2.est, states.est, states2.est, more.est, more2.est, tot_mj.est, tot_mj2.est)
se = c(safe.se, safe2.se, states.se, states2.se, more.se, more2.se, tot_mj.se, tot_mj2.se)
lower.conf = est - 1.96*se
upper.conf = est + 1.96*se

type = c(rep("SAFE Act", 2), rep("STATES Act", 2), rep("MORE Act",2), rep("Composite",2))
covs = rep(c("N","Y"), 4)
x = c(.9,1.1,1.9,2.1,2.9,3.1,3.9,4.1)
x <- c(.95, 1.05, 1.95, 2.05, 2.95, 3.05, 3.95, 4.05)
order = (c(rep(1,2), rep(2,2),rep(3,2),rep(4,2)))
plotdata = data.frame(est, lower.conf, upper.conf, covs, type,order)
plotdata = plotdata[order(-order),]
plotdata = data.frame(plotdata, x)
plotdata$covariates <- ifelse(plotdata$covs=="N", "No", "Yes")

#plotting
figA3 <- ggplot(plotdata, aes(x = x, y = est))+
  geom_point(size=3, aes(shape = covariates, color = covariates))+
  geom_linerange(aes(ymin=lower.conf, ymax=upper.conf, color=covariates), linetype = 1, size = 1)+
  coord_flip()+
  geom_hline(yintercept = 0, linetype = "dashed")+
  theme_bw()+
  labs(x = "", y = "")+
  scale_y_continuous(breaks=c(0,.25,.5,.75,1))+
  scale_x_continuous(breaks = 1:4, labels = c("Sponsorship \n score", "MORE", "STATES", "SAFE"))+
  theme(text = element_text(size=16))+
  scale_color_manual(values=c('grey','black'))+
  theme(legend.position = "right")
figA3


##### TREATMENT AS ADULT-USE ALTERNATIVE SPECIFICATION (FIGA4) #####
dat116$adult_use <- dat116$status==2
dat116$medical_use <- dat116$status==1
dat116$prohibition <- dat116$status==0

#bill sponsorship score
tot_mj <- ivreg(tot_mj ~ adult_use | referenda , data = dat116)
summary(tot_mj, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(tot_mj, dat116$abb)
tot_mj.est = cluster.robust.se(tot_mj, dat116$abb)[2,1]
tot_mj.se = cluster.robust.se(tot_mj, dat116$abb)[2,2]

#safe banking act
safe <- ivreg(hr1595_sponsor ~ adult_use | referenda , data = dat116)
summary(safe, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(safe, dat116$abb)
safe.est = cluster.robust.se(safe, dat116$abb)[2,1]
safe.se = cluster.robust.se(safe, dat116$abb)[2,2]

#states act
states <- ivreg(hr2093_sponsor ~ adult_use | referenda , data = dat116)
summary(states, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(states, dat116$abb)
states.est = cluster.robust.se(states, dat116$abb)[2,1]
states.se = cluster.robust.se(states, dat116$abb)[2,2]

#more act
more <- ivreg(hr3884_sponsor ~ adult_use | referenda , data = dat116)
summary(more, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(more, dat116$abb)
more.est = cluster.robust.se(more, dat116$abb)[2,1]
more.se = cluster.robust.se(more, dat116$abb)[2,2]


### with covariates ###
dat116.est <- dat116 %>%
  select(tot_mj, hr1595_sponsor, hr2093_sponsor, hr3884_sponsor, 
         adult_use, dem, referenda, nominate_dim1, nominate_dim2, pres_dem_2016, social_lib2000s, abb)

#bill sponsorship score
tot_mj2 <- ivreg(tot_mj ~ adult_use + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s | 
                   referenda + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, data = na.omit(dat116.est))
summary(tot_mj2, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(tot_mj2, na.omit(dat116.est)$abb)
tot_mj2.est = cluster.robust.se(tot_mj2, na.omit(dat116.est)$abb)[2,1]
tot_mj2.se = cluster.robust.se(tot_mj2, na.omit(dat116.est)$abb)[2,2]

#safe banking act
safe2 <- ivreg(hr1595_sponsor ~ adult_use + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s | 
                 referenda + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, data = na.omit(dat116.est))
summary(safe2, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(safe2, na.omit(dat116.est)$abb)
safe2.est = cluster.robust.se(safe2, na.omit(dat116.est)$abb)[2,1]
safe2.se = cluster.robust.se(safe2, na.omit(dat116.est)$abb)[2,2]


#states act
states2 <- ivreg(hr2093_sponsor ~ adult_use + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s | 
                   referenda + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, data = na.omit(dat116.est))
summary(states2, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(states2, na.omit(dat116.est)$abb)
states2.est = cluster.robust.se(states2, na.omit(dat116.est)$abb)[2,1]
states2.se = cluster.robust.se(states2, na.omit(dat116.est)$abb)[2,2]

#more act
more2 <- ivreg(hr3884_sponsor ~ adult_use + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s | 
                 referenda + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, data = na.omit(dat116.est))
summary(more2, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(more2, na.omit(dat116.est)$abb)
more2.est = cluster.robust.se(more2, na.omit(dat116.est)$abb)[2,1]
more2.se = cluster.robust.se(more2, na.omit(dat116.est)$abb)[2,2]


#preparing to plot 
est = c(safe.est, safe2.est, states.est, states2.est, more.est, more2.est, tot_mj.est, tot_mj2.est)
se = c(safe.se, safe2.se, states.se, states2.se, more.se, more2.se, tot_mj.se, tot_mj2.se)
lower.conf = est - 1.96*se
upper.conf = est + 1.96*se

type = c(rep("SAFE Act", 2), rep("STATES Act", 2), rep("MORE Act",2), rep("Composite",2))
covs = rep(c("N","Y"), 4)
x = c(.9,1.1,1.9,2.1,2.9,3.1,3.9,4.1)
x <- c(.95, 1.05, 1.95, 2.05, 2.95, 3.05, 3.95, 4.05)
order = (c(rep(1,2), rep(2,2),rep(3,2),rep(4,2)))
plotdata = data.frame(est, se, lower.conf, upper.conf, covs, type,order)
plotdata = plotdata[order(-order),]
plotdata = data.frame(plotdata, x)
plotdata$covariates <- ifelse(plotdata$covs=="N", "No", "Yes")

#plotting
figA4 <- ggplot(plotdata, aes(x = x, y = est))+
  geom_point(size=3, aes(shape = covariates, color = covariates))+
  geom_linerange(aes(ymin=lower.conf, ymax=upper.conf, color=covariates), linetype = 1, size = 1)+
  coord_flip()+
  geom_hline(yintercept = 0, linetype = "dashed")+
  theme_bw()+
  labs(x = "", y = "")+
  scale_y_continuous(breaks=c(0,.25,.5,.75,1))+
  scale_x_continuous(breaks = 1:4, labels = c("Sponsorship \n score", "MORE", "STATES", "SAFE"))+
  theme(text = element_text(size=16))+
  scale_color_manual(values=c('grey','black'))+
  theme(legend.position = "right")
figA4



##### HETEROGENEOUS EFFECTS BY PID (FIGA5) #####

dat116dem <- subset(dat116, party=="D")
dat116rep <- subset(dat116, party=="R")

### among dems ###

#bill sponsorship score
tot_mj <- ivreg(tot_mj ~ status | referenda , data = dat116dem)
summary(tot_mj, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(tot_mj, dat116dem$abb)
tot_mj.est = cluster.robust.se(tot_mj, dat116dem$abb)[2,1]
tot_mj.se = cluster.robust.se(tot_mj, dat116dem$abb)[2,2]

#safe banking act
safe <- ivreg(hr1595_sponsor ~ status | referenda , data = dat116dem)
summary(safe, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(safe, dat116dem$abb)
safe.est = cluster.robust.se(safe, dat116dem$abb)[2,1]
safe.se = cluster.robust.se(safe, dat116dem$abb)[2,2]

#states act
states <- ivreg(hr2093_sponsor ~ status | referenda , data = dat116dem)
summary(states, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(states, dat116dem$abb)
states.est = cluster.robust.se(states, dat116dem$abb)[2,1]
states.se = cluster.robust.se(states, dat116dem$abb)[2,2]

#more act
more <- ivreg(hr3884_sponsor ~ status | referenda , data = dat116dem)
summary(more, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(more, dat116dem$abb)
more.est = cluster.robust.se(more, dat116dem$abb)[2,1]
more.se = cluster.robust.se(more, dat116dem$abb)[2,2]


### with covariates ###

#bill sponsorship score
tot_mj2 <- ivreg(tot_mj ~ status + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s | 
                   referenda + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, data = dat116dem)
summary(tot_mj2, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(tot_mj2, dat116dem$abb)
tot_mj2.est = cluster.robust.se(tot_mj2, dat116dem$abb)[2,1]
tot_mj2.se = cluster.robust.se(tot_mj2, dat116dem$abb)[2,2]

#safe banking act
safe2 <- ivreg(hr1595_sponsor ~ status + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s | 
                 referenda + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, data = dat116dem)
summary(safe2, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(safe2, dat116dem$abb)
safe2.est = cluster.robust.se(safe2, dat116dem$abb)[2,1]
safe2.se = cluster.robust.se(safe2, dat116dem$abb)[2,2]

#states act
states2 <- ivreg(hr2093_sponsor ~ status + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s | 
                   referenda + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, data = dat116dem)
summary(states2, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(states2, dat116dem$abb)
states2.est = cluster.robust.se(states2, dat116dem$abb)[2,1]
states2.se = cluster.robust.se(states2, dat116dem$abb)[2,2]

#more act
more2 <- ivreg(hr3884_sponsor ~ status + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s | 
                 referenda + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, data = dat116dem)
summary(more2, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(more2, dat116dem$abb)
more2.est = cluster.robust.se(more2, dat116dem$abb)[2,1]
more2.se = cluster.robust.se(more2, dat116dem$abb)[2,2]

#preparing to plot 
est = c(safe.est, safe2.est, states.est, states2.est, more.est, more2.est, tot_mj.est, tot_mj2.est)
se = c(safe.se, safe2.se, states.se, states2.se, more.se, more2.se, tot_mj.se, tot_mj2.se)
lower.conf = est - 1.96*se
upper.conf = est + 1.96*se

type = c(rep("SAFE Act", 2), rep("STATES Act", 2), rep("MORE Act",2), rep("Composite",2))
covs = rep(c("No","Yes"), 4)
x = c(.9,1.1,1.9,2.1,2.9,3.1,3.9,4.1)
order = (c(rep(1,2), rep(2,2),rep(3,2),rep(4,2)))
party = rep("Democrat", 8)
plotdata = data.frame(est, lower.conf, upper.conf, covs, type,order, party)
plotdata = plotdata[order(-order),]
plotdata = data.frame(plotdata, x)
plotdata.dem <- plotdata

### among reps ###

#bill sponsorship score
tot_mj <- ivreg(tot_mj ~ status | referenda , data = dat116rep)
summary(tot_mj, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(tot_mj, dat116rep$abb)
tot_mj.est = cluster.robust.se(tot_mj, dat116rep$abb)[2,1]
tot_mj.se = cluster.robust.se(tot_mj, dat116rep$abb)[2,2]

#safe banking act
safe <- ivreg(hr1595_sponsor ~ status | referenda , data = dat116rep)
summary(safe, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(safe, dat116rep$abb)
safe.est = cluster.robust.se(safe, dat116rep$abb)[2,1]
safe.se = cluster.robust.se(safe, dat116rep$abb)[2,2]

#states act
states <- ivreg(hr2093_sponsor ~ status | referenda , data = dat116rep)
summary(states, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(states, dat116rep$abb)
states.est = cluster.robust.se(states, dat116rep$abb)[2,1]
states.se = cluster.robust.se(states, dat116rep$abb)[2,2]

#more act
more <- ivreg(hr3884_sponsor ~ status | referenda , data = dat116rep)
summary(more, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(more, dat116rep$abb)
more.est = cluster.robust.se(more, dat116rep$abb)[2,1]
more.se = cluster.robust.se(more, dat116rep$abb)[2,2]


### with covariates ###

#bill sponsorship score
tot_mj2 <- ivreg(tot_mj ~ status + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s | 
                   referenda + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, data = dat116rep)
summary(tot_mj2, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(tot_mj2, dat116rep$abb)
tot_mj2.est = cluster.robust.se(tot_mj2, dat116rep$abb)[2,1]
tot_mj2.se = cluster.robust.se(tot_mj2, dat116rep$abb)[2,2]

#safe banking act
safe2 <- ivreg(hr1595_sponsor ~ status + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s | 
                 referenda + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, data = dat116rep)
summary(safe2, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(safe2, dat116rep$abb)
safe2.est = cluster.robust.se(safe2, dat116rep$abb)[2,1]
safe2.se = cluster.robust.se(safe2, dat116rep$abb)[2,2]

#states act
states2 <- ivreg(hr2093_sponsor ~ status + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s | 
                   referenda + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, data = dat116rep)
summary(states2, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(states2, dat116rep$abb)
states2.est = cluster.robust.se(states2, dat116rep$abb)[2,1]
states2.se = cluster.robust.se(states2, dat116rep$abb)[2,2]

#more act
more2 <- ivreg(hr3884_sponsor ~ status + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s | 
                 referenda + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, data = dat116rep)
summary(more2, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(more2, dat116rep$abb)
more2.est = cluster.robust.se(more2, dat116rep$abb)[2,1]
more2.se = cluster.robust.se(more2, dat116rep$abb)[2,2]

#preparing to plot 
est = c(safe.est, safe2.est, states.est, states2.est, more.est, more2.est, tot_mj.est, tot_mj2.est)
se = c(safe.se, safe2.se, states.se, states2.se, more.se, more2.se, tot_mj.se, tot_mj2.se)
lower.conf = est - 1.96*se
upper.conf = est + 1.96*se

type = c(rep("SAFE Act", 2), rep("STATES Act", 2), rep("MORE Act",2), rep("Composite",2))
covs = rep(c("No","Yes"), 4)
x = c(.9,1.1,1.9,2.1,2.9,3.1,3.9,4.1)
order = (c(rep(1,2), rep(2,2),rep(3,2),rep(4,2)))
party = rep("Republican", 8)
plotdata = data.frame(est, lower.conf, upper.conf, covs, type,order, party)
plotdata = plotdata[order(-order),]
plotdata = data.frame(plotdata, x)
plotdata.rep <- plotdata

#append
plotdata <- rbind(plotdata.dem, plotdata.rep)
plotdata$covariates <- plotdata$covs

#plotting
figA5 <- ggplot(plotdata, aes(x = x, y = est))+
  geom_point(size=3, aes(shape = covariates, color = covariates))+
  geom_linerange(aes(ymin=lower.conf, ymax=upper.conf, color=covariates), linetype = 1, size = 1)+
  coord_flip()+
  facet_wrap(~party)+
  geom_hline(yintercept = 0, linetype = "dashed")+
  theme_bw()+
  labs(x = "", y = "")+
  scale_y_continuous(breaks=c(0,.25,.5,.75,1))+
  scale_x_continuous(breaks = 1:4, labels = c("Sponsorship \n score", "MORE", "STATES", "SAFE"))+
  scale_color_manual(values=c('grey','black'))+
  theme(text = element_text(size=16))+
  theme(legend.position = "right")
figA5

##### COVARIATE ADJUSTMENT SPECIFICATION (FIGA6) #####

#bill sponsorship score
tot_mj <- lm_robust(tot_mj ~ status + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, cluster=abb, data = dat116)
tot_mj.est = coeftest(tot_mj)[2,1]
tot_mj.se = coeftest(tot_mj)[2,2]

#safe banking act
safe <- lm_robust(hr1595_sponsor ~ status + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, cluster=abb, data = dat116)
safe.est = coeftest(safe)[2,1]
safe.se = coeftest(safe)[2,2]

#states act
states <- lm_robust(hr2093_sponsor ~ status + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, cluster=abb, data = dat116)
states.est = coeftest(states)[2,1]
states.se = coeftest(states)[2,2]

#more act
more <- lm_robust(hr3884_sponsor ~ status + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, cluster=abb, data = dat116)
more.est = coeftest(more)[2,1]
more.se = coeftest(more)[2,2]

#plotting
est = c(safe.est, states.est, more.est, tot_mj.est)
se = c(safe.se, states.se, more.se, tot_mj.se)
lower.conf = est - 1.96*se
upper.conf = est + 1.96*se
x = c(1:4)
order = c(1:4)
plotdata = data.frame(est, lower.conf, upper.conf, order)
plotdata = plotdata[order(-order),]
plotdata = data.frame(plotdata, x)

#ploting
figA6 = ggplot(plotdata, aes(x = x, y = est))+
  geom_point(size=3)+
  scale_shape_manual(values=c(1, 2))+
  geom_linerange(aes(ymin=lower.conf, ymax=upper.conf), linetype = 1, size = 1)+
  coord_flip()+
  geom_hline(yintercept = 0, linetype = "dashed")+
  theme_bw()+
  labs(x = "", y = "")+
  scale_y_continuous(breaks=c(0,.25,.5,.75,1))+
  scale_x_continuous(breaks = 1:4, labels = c("Sponsorship \n score", "MORE", "STATES", "SAFE"))+
  theme(text = element_text(size=16))+
  theme(legend.position = "none")
figA6

##### FALSIFICATION TEST (FIG 4) #####

#sponsorship data from prior Congresses
df97 <- import("cleaned_097.dta")
df97$abb <- df97$state
df97 <- left_join(df97, policy.dat.2020, by = c("abb"="abb"))
reg97 <- lm(c_097_sponsor ~ referenda, data = df97)
summary(reg97)
reg97.est <- coeftest(reg97, cluster.vcov(reg97, df97$abb))[2,1]
reg97.se <- coeftest(reg97, cluster.vcov(reg97, df97$abb))[2,2]

df98 <- import("cleaned_098.dta")
df98$abb <- df98$state
df98 <- left_join(df98, policy.dat.2020, by = c("abb"="abb"))
reg98 <- lm(c_098_sponsor ~ referenda, data = df98)
summary(reg98)
reg98.est <- coeftest(reg98, cluster.vcov(reg98, df98$abb))[2,1]
reg98.se <- coeftest(reg98, cluster.vcov(reg98, df98$abb))[2,2]

df99 <- import("cleaned_099.dta")
df99$abb <- df99$state
df99 <- left_join(df99, policy.dat.2020, by = c("abb"="abb"))
reg99 <- lm(c_099_sponsor ~ referenda, data = df99)
summary(reg99)
reg99.est <- coeftest(reg99, cluster.vcov(reg99, df99$abb))[2,1]
reg99.se <- coeftest(reg99, cluster.vcov(reg99, df99$abb))[2,2]


#plotting 
est = c(reg97.est, reg98.est, reg99.est)
se = c(reg97.se, reg98.se, reg99.se)
congress = c("97th \n Congress", "98th \n Congress","99th \n Congress")
order = c(1:3)
x = c(1:3)
plotdata = data.frame(est, se, congress,order)
plotdata = plotdata[order(-order),]
plotdata = data.frame(plotdata, x)

fals.plot = ggplot(plotdata, aes(x = x, y = est))+
  geom_point(size=3)+
  geom_linerange(aes(ymin=est-1.96*se, ymax=est+1.96*se), linetype = 1, size = 1, color = "black")+
  coord_flip()+
  geom_hline(yintercept = 0, linetype = "dashed")+
  theme_bw()+
  labs(x = "", y = "")+
  scale_y_continuous(breaks=c(-.25,0,.25))+
  ylim(-.2,.2)+
  scale_x_continuous(breaks = 3:1, labels = congress)+
  theme(legend.position = "none")+
  theme(text = element_text(size=16))+
  ggtitle("Rescheduling")
fals.plot



#compared to 116th Congress major bills
regsafe <- lm(hr1595_sponsor ~ referenda, data = dat116)
summary(regsafe)
regsafe.est <- coeftest(regsafe, cluster.vcov(regsafe, dat116$abb))[2,1]
regsafe.se <- coeftest(regsafe, cluster.vcov(regsafe, dat116$abb))[2,2]

regstates <- lm(hr2093_sponsor ~ referenda, data = dat116)
summary(regstates)
regstates.est <- coeftest(regstates, cluster.vcov(regstates, dat116$abb))[2,1]
regstates.se <- coeftest(regstates, cluster.vcov(regstates, dat116$abb))[2,2]

regmore <- lm(hr3884_sponsor ~ referenda, data = dat116)
summary(regmore)
regmore.est <- coeftest(regmore, cluster.vcov(regmore, dat116$abb))[2,1]
regmore.se <- coeftest(regmore, cluster.vcov(regmore, dat116$abb))[2,2]

#plotting 
est = c(regsafe.est, regstates.est, regmore.est)
se = c(regsafe.se, regstates.se, regmore.se)
lower.conf = est - 1.96*se
upper.conf = est + 1.96*se
bill = c("SAFE", "STATES","MORE")
x = c(3:1)
plotdata = data.frame(est, lower.conf, upper.conf, bill,x)

fals.compare.plot = ggplot(plotdata, aes(x = x, y = est))+
  geom_point(size=3)+
  geom_linerange(aes(ymin=lower.conf, ymax=upper.conf), linetype = 1, size = 1, color = "black")+
  coord_flip()+
  geom_hline(yintercept = 0, linetype = "dashed")+
  theme_bw()+
  labs(x = "", y = "")+
  scale_y_continuous(breaks=c(-.5,0,.5))+
  ylim(-.4,.4)+
  scale_x_continuous(breaks = 1:3, labels = c("MORE","STATES","SAFE"))+
  theme(legend.position = "none")+
  theme(text = element_text(size=16))+
  ggtitle("116th Congress")
fals.compare.plot

#putting together
fig4 <- grid.arrange(fals.plot, fals.compare.plot, ncol=2)
fig4

#################################
#### ROLL CALL RESULTS ################
################################

#House members
members <- import("H116_members.csv")  %>%
  dplyr::select(bioguide_id)

#join with sponsorship data
members <- left_join(members, dat116, by=c("bioguide_id"))
rolls <- import("cleaned_rolls.dta") %>%
  dplyr::select(bioguide_id, bill, vote)
rolls116 <- left_join(rolls, members, by=c("bioguide_id"))

##### IV ANALYSIS (FIG5) #####

rolls116$dem = ifelse(rolls116$party == "D", 1, 0)
rolls116$vote2 <- ifelse(is.na(rolls116$vote), 0, #abstentions as 0
                         ifelse(rolls116$vote==0, 0, 1))

#subsetting to needed variables
rolls116.est <- rolls116 %>%
  dplyr::select(vote2, status, bill, dem, referenda, nominate_dim1, nominate_dim2, pres_dem_2016, social_lib2000s, abb) %>%
  na.omit()

#regressions 370 (HR 3055: preventing DOJ interference)
roll370_1 = ivreg(vote2 ~ status | referenda, data = subset(rolls116.est, bill==370))
summary(roll370_1, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(roll370_1, subset(rolls116.est, bill==370)$abb)
roll370_1.est = cluster.robust.se(roll370_1, subset(rolls116.est, bill==370)$abb)[2,1]
roll370_1.se = cluster.robust.se(roll370_1, subset(rolls116.est, bill==370)$abb)[2,2]

roll370_2 = ivreg(vote2 ~ status + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s | 
                    referenda + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, data = subset(rolls116.est, bill==370))
summary(roll370_2, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(roll370_2, subset(rolls116.est, bill==370)$abb)
roll370_2.est = cluster.robust.se(roll370_2, subset(rolls116.est, bill==370)$abb)[2,1]
roll370_2.se = cluster.robust.se(roll370_2, subset(rolls116.est, bill==370)$abb)[2,2]

#regressions 544 (SAFE Banking Act)
roll544_1 = ivreg(vote2 ~ status | referenda, data = subset(rolls116.est, bill==544))
summary(roll544_1, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(roll544_1, subset(rolls116.est, bill==544)$abb)
roll544_1.est = cluster.robust.se(roll544_1, subset(rolls116.est, bill==544)$abb)[2,1]
roll544_1.se = cluster.robust.se(roll544_1, subset(rolls116.est, bill==544)$abb)[2,2]

roll544_2 = ivreg(vote2 ~ status + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s | 
                    referenda + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, data = subset(rolls116.est, bill==544))
summary(roll544_2, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(roll544_2, subset(rolls116.est, bill==544)$abb)
roll544_2.est = cluster.robust.se(roll544_2, subset(rolls116.est, bill==544)$abb)[2,1]
roll544_2.se = cluster.robust.se(roll544_2, subset(rolls116.est, bill==544)$abb)[2,2]

#MORE Act
roll235_1 = ivreg(vote2 ~ status | referenda, data = subset(rolls116.est, bill==235))
summary(roll235_1, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(roll235_1, subset(rolls116.est, bill==235)$abb)
roll235_1.est = cluster.robust.se(roll235_1, subset(rolls116.est, bill==235)$abb)[2,1]
roll235_1.se = cluster.robust.se(roll235_1, subset(rolls116.est, bill==235)$abb)[2,2]

roll235_2 = ivreg(vote2 ~ status + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s | 
                    referenda + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, data = subset(rolls116.est, bill==235))
summary(roll235_2, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(roll235_2, subset(rolls116.est, bill==235)$abb)
roll235_2.est = cluster.robust.se(roll235_2, subset(rolls116.est, bill==235)$abb)[2,1]
roll235_2.se = cluster.robust.se(roll235_2, subset(rolls116.est, bill==235)$abb)[2,2]

#second DOJ
roll174_1 = ivreg(vote2 ~ status | referenda, data = subset(rolls116.est, bill==174))
summary(roll174_1, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(roll174_1, subset(rolls116.est, bill==174)$abb)
roll174_1.est = cluster.robust.se(roll174_1, subset(rolls116.est, bill==174)$abb)[2,1]
roll174_1.se = cluster.robust.se(roll174_1, subset(rolls116.est, bill==174)$abb)[2,2]

roll174_2 = ivreg(vote2 ~ status + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s | 
                    referenda + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, data = subset(rolls116.est, bill==174))
summary(roll174_2, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(roll174_2, subset(rolls116.est, bill==174)$abb)
roll174_2.est = cluster.robust.se(roll174_2, subset(rolls116.est, bill==174)$abb)[2,1]
roll174_2.se = cluster.robust.se(roll174_2, subset(rolls116.est, bill==174)$abb)[2,2]

### preparing to plot
est <- c(roll370_1.est, roll370_2.est, roll174_1.est, roll174_2.est ,roll544_1.est, roll544_2.est, roll235_1.est, roll235_2.est)
se <- c(roll370_1.se, roll370_2.se, roll174_1.se, roll174_2.se, roll544_1.se, roll544_2.se, roll235_1.se, roll235_2.se)
lower.conf = est - 1.96*se
upper.conf = est + 1.96*se
covs = rep(c("N","Y"), 4)
x = rev(c(.9,1.1,1.9,2.1, 2.9, 3.1, 3.9, 4.1))
plotdata = data.frame(est, lower.conf, upper.conf, covs, x)
plotdata$covariates <- ifelse(plotdata$covs=="N", "No", "Yes")

#plotting
fig5 = ggplot(plotdata, aes(x = x, y = est))+
  geom_point(size=3, aes(shape = covariates, color = covariates))+
  geom_linerange(aes(ymin=lower.conf, ymax=upper.conf, color=covariates), linetype = 1, size = 1)+
  coord_flip()+
  geom_hline(yintercept = 0, linetype = "dashed")+
  theme_bw()+
  labs(x = "", y = "")+
  scale_y_continuous(breaks=c(0,.25,.5,.75,1))+
  scale_x_continuous(breaks = 1:4, labels = c("MORE", "SAFE", "DOJ interference \n amendment (2019)", "DOJ interference \n amendment (2020)"))+
  scale_color_manual(values=c('grey','black'))+
  theme(text = element_text(size=16))+
  theme(legend.position = "right")
fig5

##### COVARIATE ADJUSTMENT SPECIFICATION (FIGA7) #####

roll370 <- lm_robust(vote2 ~ status + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, cluster=abb, data = subset(rolls116.est, bill==370))
roll370.est = coeftest(roll370)[2,1]
roll370.se = coeftest(roll370)[2,2]

roll544 <- lm_robust(vote2 ~ status + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, cluster=abb, data = subset(rolls116.est, bill==544))
roll544.est = coeftest(roll544)[2,1]
roll544.se = coeftest(roll544)[2,2]

roll235 <- lm_robust(vote2 ~ status + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, cluster=abb, data = subset(rolls116.est, bill==235))
roll235.est = coeftest(roll235)[2,1]
roll235.se = coeftest(roll235)[2,2]

roll174 <- lm_robust(vote2 ~ status + dem + nominate_dim1 + nominate_dim2 + pres_dem_2016 + social_lib2000s, cluster=abb, data = subset(rolls116.est, bill==174))
roll174.est = coeftest(roll174)[2,1]
roll174.se = coeftest(roll174)[2,2]


#plotting
est = c(roll370.est, roll174.est, roll544.est, roll235.est)
se = c(roll370.se, roll174.se, roll544.se, roll235.se)
lower.conf = est - 1.96*se
upper.conf = est + 1.96*se
x = c(1:4)
order = c(1:4)
plotdata = data.frame(est, lower.conf, upper.conf, order)
plotdata = plotdata[order(-order),]
plotdata = data.frame(plotdata, x)

figA7 <- ggplot(plotdata, aes(x = x, y = est))+
  geom_point(size=3)+
  scale_shape_manual(values=c(1, 2))+
  geom_linerange(aes(ymin=lower.conf, ymax=upper.conf), linetype = 1, size = 1)+
  coord_flip()+
  geom_hline(yintercept = 0, linetype = "dashed")+
  theme_bw()+
  labs(x = "", y = "")+
  scale_y_continuous(breaks=c(0,.25,.5,.75,1))+
  scale_x_continuous(breaks = 1:4, labels = c("MORE", "SAFE", "DOJ interference \n amendment (2019)", "DOJ interference \n amendment (2020)"))+
  theme(text = element_text(size=16))+
  theme(legend.position = "none")
figA7


#######################################
####### PARSING MECHANISMS #######
#######################################


##### PUBLIC OPINION SHIFT? (FIG6) #####

#recreational law
est_rec_law<-gsynth(marijuana_weighted ~ rec_law + mass_liberalism_econ + mass_liberalism_soc,
                    data = policy.dat, index = c("abb", "year"), se = T, min.T0 = 6,
                    inference = "parametric", r = 0, CV = FALSE, force = "two-way",
                    nboots = 1000, seed = 02139,
                    na.rm=T)
plot_rec_law <- plot(est_rec_law, type = "counterfactual", xlim = c(-5, 5), theme.bw = T, main = "Adult-use", ylim = c(0,1), ylab = "% Support for marijuana legalization", 
                     xlab = "Years relative to treatment", legendOff = T, cex.main=1.5, cex.lab=1.5, cex.axis=1.5) #note: dashed is control!

#medical law
est_medical_law<-gsynth(marijuana_weighted ~ medical_law + mass_liberalism_econ + mass_liberalism_soc,
                        data = policy.dat, index = c("abb", "year"), se = T, min.T0 = 6,
                        inference = "parametric", r = 0, CV = FALSE, force = "two-way",
                        nboots = 1000, seed = 02139,
                        na.rm=T)
plot_medical_law <- plot(est_medical_law, type = "counterfactual", xlim = c(-5, 5), theme.bw = T, main = "Medical", ylim = c(0,1), ylab = "",
                         xlab = "Years relative to treatment", legendOff = T, , cex.main=1.5, cex.lab=1.5, cex.axis=1.5) #note: dashed is control!

fig6 <- grid.arrange(plot_rec_law, plot_medical_law, ncol=2)
fig6


##### INTEREST GROUPS #####

#campaign contributions
fin.data <- import("mj_finance_analysis.dta")
fin.data$year <- 2018
fin.data <- subset(fin.data, select = -referenda)
fin.data <- left_join(fin.data, policy.dat, by = c("abb","year"))
fin.data$chamber <- ifelse(fin.data$senate==1, "Senate", "House")

#proportion of races by state with mj participation 
fin.plot <- fin.data %>%
  group_by(chamber, status) %>%
  dplyr::summarize(proportion = mean(mj, na.rm=T))
# myColors = c("#4699dd", "#56ddc5")

#plot
figA10 <- ggplot(data = fin.plot, aes(x = status, y = proportion)) +
  geom_bar(stat = "identity")+
  facet_wrap(~chamber)+
  labs(x = "", y = "Proportion")+
  theme_bw()+
  theme(text = element_text(size=16))+
  scale_x_continuous(breaks = 0:2, labels = c("No Legal \n Marijuana","Medical","Adult-Use"))+
  theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1))+
  ylim(0,.3)+
  theme(legend.position = "none")
figA10

# IV estimates
house <- ivreg(mj ~ status | referenda, data = subset(fin.data, chamber == "House"))
summary(house, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(house, fin.data[fin.data$chamber=="House",]$abb)
house.est = cluster.robust.se(house, fin.data[fin.data$chamber=="House",]$abb)[2,1]
house.se = cluster.robust.se(house, fin.data[fin.data$chamber=="House",]$abb)[2,2]

senate <- ivreg(mj ~ status | referenda, data = subset(fin.data, chamber == "Senate"))
summary(senate, vcov = sandwich, diagnostics = TRUE)
cluster.robust.se(senate, fin.data[fin.data$chamber=="Senate",]$abb)
senate.est = cluster.robust.se(senate, fin.data[fin.data$chamber=="Senate",]$abb)[2,1]
senate.se = cluster.robust.se(senate, fin.data[fin.data$chamber=="Senate",]$abb)[2,2]

est <- c(house.est, senate.est)
se <- c(house.se, senate.se)
lower.conf = est - 1.96*se
upper.conf = est + 1.96*se
x = c(1,2)
plotdata = data.frame(est, lower.conf, upper.conf, x)

#plot
figA11 = ggplot(plotdata, aes(x = x, y = est))+
  geom_point(size = 3)+
  geom_linerange(aes(ymin=lower.conf, ymax=upper.conf), linetype = 1, size = 1,color = "black")+
  coord_flip()+
  geom_hline(yintercept = 0, linetype = "dashed")+
  theme_bw()+
  labs(x = "", y = "")+
  scale_y_continuous(breaks=c(0,.25,.5,.75,1))+
  theme(text = element_text(size=16))+
  scale_x_continuous(breaks = 1:2, labels = c("House", "Senate"))+
  theme(legend.position = "none")
figA11


##### REPRESENTATION AND REVENUE (FIGA9) #####

plotdata <- dat116 %>%
  group_by(abb, rev_per_cap) %>%
  summarize(tot_mj=mean(tot_mj, na.rm=T))

figA9 <- ggplot(data = plotdata, aes(x=rev_per_cap, y= tot_mj)) + 
  geom_point(color="blue", size = 2)   +
  geom_text_repel(aes(label=abb),size=3)   +
  xlab("Per capita industry revenue in 2018 ($)")+
  theme(text = element_text(size=16))+
  ylab("Average bill sponsorship score for members of Congress")+
  theme_minimal()
figA9


